package com.rovinj_server.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;


import com.rovinj_server.entity.Inventory;

import com.rovinj_server.entity.InventoryBarodeLocation;
import com.rovinj_server.entity.InventorySub;
import com.rovinj_server.entity.InventoryTask;
import com.rovinj_server.entity.InventoryTaskSub;
import com.rovinj_server.entity.SubmitTask;
import com.rovinj_server.utisl.HappySQL;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * author Bro0cL on 2016/1/26.
 */
public class DBManager {
    private static final String ASSETS_NAME = "china_cities.db";
    private static final String DB_NAME = "china_cities.db";
    private static final String INVENTORY = "Inventory"; // 盘点单
    private static final String INVENTORYSUB = "InventorySub"; //盘点单明细
    private static final String  INVENTORYTASK = "InventoryTask";  //任务单号
    private static final String INVENTORYTASKSUB = "InventoryTaskSub"; //任务明细
    private static final String SUBMITTASK = "SubmitTask";
    private static final String SUBMITTASKBARODE = "SubmitTaskBarode";
    private static final int BUFFER_SIZE = 1024;
    private String DB_PATH;
    private Context mContext;

    public DBManager(Context context) {
        this.mContext = context;
        DB_PATH = File.separator + "data"
                + Environment.getDataDirectory().getAbsolutePath() + File.separator
                + context.getPackageName() + File.separator + "databases" + File.separator;
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public void copyDBFile() {
        File dir = new File(DB_PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File dbFile = new File(DB_PATH + DB_NAME);
        if (!dbFile.exists()) {
            InputStream is;
            OutputStream os;
            try {
                is = mContext.getResources().getAssets().open(ASSETS_NAME);
                os = new FileOutputStream(dbFile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int length;
                while ((length = is.read(buffer, 0, buffer.length)) > 0) {
                    os.write(buffer, 0, length);
                }
                os.flush();
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 清除所有记录
     **/
    public int dedeteData(int qty) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        int num = 0;
        if (qty == 1) { //清除盘点单
            num = num + db.delete(INVENTORY, null, null);
            num = num + db.delete(INVENTORYSUB, null, null);
        } else if (qty == 2) { //清除任务
            num = num + db.delete(INVENTORYTASK, null, null);
            num = num + db.delete(INVENTORYTASKSUB, null, null);
        } else if (qty == 3) {//清除所有
            num = num + db.delete(INVENTORY, null, null);
            num = num + db.delete(INVENTORYSUB, null, null);
            num = num + db.delete(INVENTORYTASK, null, null);
            num = num + db.delete(INVENTORYTASKSUB, null, null);
        }


        return num;
    }

    /**
     * 更新数据 状态
     **/
    public void updateData(String id, String state) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYTASKSUB + " set state ='" + state + "' where id ='" + id + "'");
    }

    /**
     * 更新数据 数量 update InventoryTaskSub  set discNumber =discNumber + 1 where id ='3'
     **/
    public void updateQtyData(String barode) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set discNumber =discNumber + " + 1 + "  where barode ='" + barode + "'");
        System.out.println("-----------");
    }

    /**
     * 更新数据 数量 update InventoryTaskSub  set discNumber =discNumber + 1 where id ='3'
     **/
    public void updateQtyData(String barode, String state) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set discNumber =discNumber + " + 1 + " , state='" + state + "'  where barode ='" + barode + "'");
        System.out.println("-----------");
    }

    /**
     * 更新数据 数量
     **/
    public void updateState(String barode, String state) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set state  = '" + state + "'    where barode ='" + barode + "'");
        System.out.println("-----------");
    }

    /**
     * 更新数据 数量
     **/
    public void updateQtyjia(String barode, String ziduan) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set " + ziduan + " = " + ziduan + " + 1   where barode ='" + barode + "'");
        System.out.println("-----------");
    }

    public void updateQtyJin(String barode, String ziduan) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set " + ziduan + " = " + ziduan + " - 1   where barode ='" + barode + "'");
        System.out.println("-----------");
    }

    /**
     * 更新数据 数量 update InventoryTaskSub  set discNumber =discNumber + 1 where id ='3'
     **/
    public void updateCodeData(String location, String code, String beizhu) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set discNumber =discNumber + " + 1 + " , location='" + location + " ,  beizhu='" + beizhu + "'  where barode ='" + code + "'");
        System.out.println("-----------");
    }

    /**
     * 根据ID 数据设置为0
     **/
    public void updateQtyNullData(String id) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYTASKSUB + " set discNumber =" + 0 + "  where id ='" + id + "'");
        System.out.println("-----------");
    }

    /**
     * 根据 款式 设置 货位
     **/

    public void updateCodeLocation(List<InventoryBarodeLocation> inventoryBarodeLocations) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        for (int i = 0; i < inventoryBarodeLocations.size(); i++) {
            if (i > 0) {
                String location = inventoryBarodeLocations.get(i).getLocation();
                String code = inventoryBarodeLocations.get(i).getCode();
                db.execSQL("update " + INVENTORYSUB + " set location =" + location + "  where code ='" + code + "'");
            }

        }

        System.out.println("-----------");
    }

    /**
     * 根据 款式 设置 货位
     **/

    public void updateCodeLocation(String location, String code) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set location =" + location + "  where code ='" + code + "'");
        System.out.println("-----------");
    }


    public void updateQtyNull(String barode, String code) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set discNumber =" + 0 + "  where barode ='" + barode + "'  and  code='" + code + "'");
        System.out.println("-----------");
    }

    /**重扫*/
    public void setupdateQtyNull(String barode, String code, int lessThan) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set discNumber =" + 0 + ",moreThan =" + 0 + ",lessThan =" + lessThan + "   where barode ='" + barode + "'  and  code='" + code + "'");
        System.out.println("-----------");
    }


    /**重扫*/
    public void setupdateQty(String barode, String code, int moreThan, int lessThan, int discNumber) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYSUB + " set discNumber =" + discNumber + ",moreThan =" + moreThan + ",lessThan =" + lessThan + "   where barode ='" + barode + "'  and  code='" + code + "'");
        System.out.println("-----------");
    }
    /**
     * 根据ID 删除数据
     */
    public void deleteIdNull(String id) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("delete from  " + INVENTORYTASKSUB + "   where id ='" + id + "'");
        System.out.println("-----------");
    }

    /**
     * 根据ID 删除数据
     */
    public void deleteCodeNull(String code) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("delete from  " + INVENTORYTASKSUB + "   where code ='" + code + "'");
        System.out.println("-----------");
    }

    public void deleteSubIdNull(String id) {
        int num = 0;
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("delete from  " + INVENTORYSUB + "   where cheadguid ='" + id + "'");
        System.out.println("-----------");
    }

    public void deleteInventoryIdData(String id) {
        int num = 0;
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("delete from  " + INVENTORY + "   where id ='" + id + "'");
        System.out.println("-----------");
    }

    public void updateQtyNullData(String id, int qty) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.execSQL("update " + INVENTORYTASKSUB + " set discNumber =" + qty + "  where id ='" + id + "'");
        System.out.println("-----------更新数量");
    }

    /**
     * 获取 导入盘点单号数据
     **/
    public List<Inventory> findDataList() {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORY;
        List<Inventory> mListInventory = HappySQL.sql2VOList(db, sql, Inventory.class);
        return mListInventory;
    }


    /**
     * 获取
     * 明细数据 1 - 20 条
     **/
    public List<InventorySub> findSubDataList(String id, int x, int d) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + "  where cheadguid='" + id + "'" + " limit " + x + "," + d;
        System.out.println("sql 查询20条" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    /**
     * 获取
     * 明细数据 1 - 20 条
     **/
    public List<InventorySub> findStateSubDataList(String id, String state, int x, int d) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + "  where cheadguid='" + id + "' and state='" + state + "' limit " + x + "," + d;
        System.out.println("sql 查询20条" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }


    /**
     * 获取
     * * 根据货位 查询出 已盘信息
     **/
    public List<InventorySub> findLocationStateSubDataList(String loctaion, String state) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + "  where location='" + loctaion + "' and state='" + state + "'";
        System.out.println("sql 查询20条" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    /**

     **/
    public List<InventorySub> findStateSubDataList(String state, int x, int d) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + "  where  state='" + state + "' limit " + x + "," + d;
        System.out.println("sql 查询20条" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    /**
     *
     **/
    public List<InventorySub> findStateSubDataList(String id, String state) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + "  where cheadguid='" + id + "' and state='" + state + "'";
        System.out.println("sql 已盘" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    /**
     * 查询 某字段不为空的数据  998852223837050260
     **/
    public List<InventorySub> findSubDataNull(String str) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select  " + str + "  from " + INVENTORYSUB + " where  " + str + "  is not null";
        System.out.println("sql 已盘" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    /**
     *
     **/
    public List<InventorySub> findStateSubDataList(String state) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + "  where  state='" + state + "'";
        System.out.println("sql 已盘" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    /**
     * 获取
     * 明细数据
     **/
    public List<InventorySub> findSubDataList() {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB;
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    /**
     * 获取 导出  盘点数据
     **/
    public List<SubmitTask> findSubmitDataList() {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + SUBMITTASK;
        List<SubmitTask> SubmitTask = HappySQL.sql2VOList(db, sql, SubmitTask.class);
        return SubmitTask;
    }

    /**
     * 获取 导出  盘点数据 条码
     **/
    public List<String> findSubmitBarodeDataList() {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + SUBMITTASKBARODE;
        List<String> SubmitBarode = HappySQL.sql2VOList(db, sql, String.class);
        return SubmitBarode;
    }

    public List<InventoryTask> findTaskList() { //获取List数据
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASK;
        List<InventoryTask> mListInventory = HappySQL.sql2VOList(db, sql, InventoryTask.class);
        return mListInventory;
    }


    public List<InventorySub> findListSub(int x, int d) { //获取List数据
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from  " + INVENTORYSUB + "  where location between " + x + " and " + d;
        System.out.println("sql---" + sql);
        List<InventorySub> InventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return InventorySub;
    }

    public List<InventorySub> findListSubData() { //获取List数据
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB;
        System.out.println("sql---" + sql);
        List<InventorySub> InventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return InventorySub;
    }

    /**
     * 根据单号 ID  查询 明细 ， 根据库位 查询所有  条码
     */
    public List<InventorySub> findSubListData(int id) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + " WHERE cheadguid = ? ";
        System.out.println("sql---" + sql);
        List<InventorySub> mListInventory = HappySQL.sql2VOList(db, sql, new String[]{String.valueOf(id)}, InventorySub.class);
        System.out.println("sql---" + sql);
        return mListInventory;
    }

    /**
     * 根据单号 ID  查询 明细 ， 根据库位 查询所有  条码
     */
    public List<InventorySub> findIdLocationSubListData(int id, String location) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + " WHERE cheadguid = ? and location=?";
        List<InventorySub> mListInventory = HappySQL.sql2VOList(db, sql, new String[]{String.valueOf(id), location}, InventorySub.class);
        System.out.println("sql---" + sql);
        return mListInventory;
    }


    //根据货位 查询 所有条码
    public List<InventorySub> findLocationSubListData(String location) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + " WHERE location = ? ";
        List<InventorySub> mListInventory = HappySQL.sql2VOList(db, sql, new String[]{String.valueOf(location)}, InventorySub.class);
        System.out.println("sql---" + sql);
        return mListInventory;
    }

    /**
     * 根据单号 ID  查询 明细 ， 根据库位 查询所有  条码   ，根据条码 查询款号  有没快速查询 数据库里有没 相同的 库位字段 和 条码 字段
     */
    public List<InventorySub> findIdBarodeSubListData(int id, String barode) { //获取id找 任务明细明细
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + " WHERE cheadguid = ? and barode=? ";
        List<InventorySub> mListInventory = HappySQL.sql2VOList(db, sql, new String[]{String.valueOf(id), barode}, InventorySub.class);
        System.out.println("sql---" + sql);
        return mListInventory;
    }

    /***
     * 获取任务 明细
     */
    public List<InventoryTaskSub> findTaskSubList() {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASKSUB;
        List<InventoryTaskSub> mListInventory = HappySQL.sql2VOList(db, sql, InventoryTaskSub.class);
        return mListInventory;
    }

    public List<InventorySub> findSubTaskList() {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASKSUB;
        List<InventorySub> mListInventory = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mListInventory;
    }

    /***
     * 获取任务 相同 明细
     */
    public List<InventoryTaskSub> findTaskSubListXiang() {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASKSUB + " a INNER JOIN (SELECT * FROM InventoryTaskSub GROUP BY code,barode HAVING count(*) > 1) AS temp ON a.code = temp.location AND a.barode = temp.barode   ORDER BY code,barode";
        List<InventoryTaskSub> mListInventory = HappySQL.sql2VOList(db, sql, InventoryTaskSub.class);
        return mListInventory;
    }

    public InventoryTaskSub findIDTaskSubLis(String id) { //获取id找 任务明细明细
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASKSUB + " WHERE id = ?";
        System.out.println("sql---" + sql);
        InventoryTaskSub mListInventory = (InventoryTaskSub) HappySQL.sql2VO(db, sql, new String[]{String.valueOf(id)}, InventoryTaskSub.class);
        return mListInventory;
    }

    /****
     * 根据条码查找 货号
     **/
    public InventorySub findBarodeSub(String barode) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + " WHERE barode = ?";
        System.out.println("sql---" + sql);
        InventorySub mListInventory = (InventorySub) HappySQL.sql2VO(db, sql, new String[]{String.valueOf(barode)}, InventorySub.class);
        return mListInventory;
    }

    /****
     * 根据条码查找 货号
     **/
 /*   public InventorySub findBarodeSub(String barode, String state) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + "  where barode='" + barode + "' and state='" + state + "'";
        System.out.println("sql---" + sql);
        InventorySub mListInventory = (InventorySub) HappySQL.sql2VO(db, sql, new String[]{String.valueOf(barode),state}, InventorySub.class);
        return mListInventory;
    }*/
    public List<InventorySub> findBarodeSub(String barode, String state) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + "  where barode like'%" + barode + "%' and state='" + state + "'";
        System.out.println("sql 查询20条" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    public List<InventorySub> findBarodeSubTaskList(String barode) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASKSUB + "  where barode like'%" + barode + "%'";
        System.out.println("sql 查询20条" + sql);
        List<InventorySub> mInventorySub = HappySQL.sql2VOList(db, sql, InventorySub.class);
        return mInventorySub;
    }

    /****
     * 根据条码查找 货号
     **/
    public InventorySub findBarodeSubTask(String barode) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + " WHERE barode = ?";
        System.out.println("sql---" + sql);
        InventorySub mListInventory = (InventorySub) HappySQL.sql2VO(db, sql, new String[]{String.valueOf(barode)}, InventorySub.class);
        return mListInventory;
    }

    /****
     * 根据条码查找 货号
     **/
    //根据货位 查询 所有条码
    public List<InventorySub> findLocationTaskSubList(String location) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASKSUB + " WHERE location = ? ";
        List<InventorySub> mListInventory = HappySQL.sql2VOList(db, sql, new String[]{String.valueOf(location)}, InventorySub.class);
        System.out.println("sql---" + sql);
        return mListInventory;
    }

    /****
     * 根据条码查找 货号
     **/
    public InventorySub findLocationTaskSub(String location) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + " WHERE location = ?";
        System.out.println("sql---" + sql);
        InventorySub mListInventory = (InventorySub) HappySQL.sql2VO(db, sql, new String[]{String.valueOf(location)}, InventorySub.class);
        return mListInventory;
    }


    public InventorySub findIDSubLis(String id) { //获取id找 任务明细明细
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASKSUB + " WHERE id = ?";
        System.out.println("sql---" + sql);
        InventorySub mListInventory = (InventorySub) HappySQL.sql2VO(db, sql, new String[]{String.valueOf(id)}, InventoryTaskSub.class);
        return mListInventory;
    }

    public List<InventoryTaskSub> findTaskSubListData(int id) { //获取id找 任务明细明细
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYTASKSUB + " WHERE cheadguid = ?";
        System.out.println("sql---" + sql);
        List<InventoryTaskSub> mListInventory = HappySQL.sql2VOList(db, sql, new String[]{String.valueOf(id)}, InventoryTaskSub.class);
        System.out.println("sql---" + sql);
        return mListInventory;
    }


    /**
     * 根据盘点单 id  查找 明细
     **/
    public List<InventorySub> findInventoryData(int id) { //获取id找 明细
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        String sql = "select * from " + INVENTORYSUB + " WHERE cheadguid = ?";
        System.out.println("sql---" + sql);
        List<InventorySub> mListInventory = HappySQL.sql2VOList(db, sql, new String[]{String.valueOf(id)}, InventorySub.class);
        return mListInventory;
    }


    /**
     * 存储 盘点 单号
     **/
    public long insertInventoryData(Inventory mInventory) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        ContentValues values = new ContentValues();
        values.put("number", mInventory.getNumber());
        values.put("depotName", mInventory.getDepotName());
        values.put("qty", mInventory.getQty());
        long rowid = db.insert(INVENTORY, null, values);
        for (int i = 0; i < mInventory.getInventorySubList().size(); i++) {
            InventorySub mInventorySub = mInventory.getInventorySubList().get(i);
            ContentValues subvalues = new ContentValues();
            subvalues.put("location", mInventorySub.getLocation());
            subvalues.put("barode", mInventorySub.getBarode());
            subvalues.put("goods", mInventorySub.getGoods());
            subvalues.put("code", mInventorySub.getCode());
            //  subvalues.put("goodsName", mInventorySub.getGoodsName());
            //  subvalues.put("color", mInventorySub.getColor());
            subvalues.put("sizeName", mInventorySub.getSizeName());
            subvalues.put("type", mInventorySub.getType());
            subvalues.put("maitype", mInventorySub.getMaitype());
            //   subvalues.put("bookNumber", mInventorySub.getBookNumber());
            subvalues.put("sjnumber", mInventorySub.getSjnumber());
            subvalues.put("diffNumber", mInventorySub.getDiffNumber());
            subvalues.put("lessThan", mInventorySub.getLessThan());
            subvalues.put("beizhu", mInventorySub.getBeizhu());
            subvalues.put("cheadguid", rowid);
            db.insert(INVENTORYSUB, null, subvalues);
        }
        return rowid;
    }

    /***
     * 服务端 存储 发布 任务数据
     **/
    public int insertInventoryData(List<InventoryTask> mInventoryTask) {
        for (int i = 0; i < mInventoryTask.size(); i++) {
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
            ContentValues values = new ContentValues();
            values.put("code", mInventoryTask.get(i).getCode());
            values.put("name", mInventoryTask.get(i).getName());
            values.put("range", mInventoryTask.get(i).getRange());
            values.put("spQty", mInventoryTask.get(i).getSpQty());
            values.put("diffQty", mInventoryTask.get(i).getDiffQty());
            values.put("state", mInventoryTask.get(i).getState());
            values.put("crDate", mInventoryTask.get(i).getCrDate());
            long rowid = db.insert(INVENTORYTASK, null, values);
            for (int j = 0; j < mInventoryTask.get(i).getInventorySubList().size(); j++) {
                InventorySub mInventorySub = mInventoryTask.get(i).getInventorySubList().get(j);
                ContentValues subvalues = new ContentValues();
                subvalues.put("location", mInventorySub.getLocation());
                subvalues.put("barode", mInventorySub.getBarode());
                subvalues.put("goods", mInventorySub.getGoods());
                subvalues.put("code", mInventorySub.getCode());
                subvalues.put("goodsName", mInventorySub.getGoodsName());
                subvalues.put("color", mInventorySub.getColor());
                subvalues.put("sizeName", mInventorySub.getSizeName());
                subvalues.put("type", mInventorySub.getType());
                subvalues.put("maitype", mInventorySub.getMaitype());
                subvalues.put("bookNumber", mInventorySub.getBookNumber());
                subvalues.put("sjnumber", mInventorySub.getSjnumber());
                subvalues.put("diffNumber", mInventorySub.getDiffNumber());
                subvalues.put("beizhu", mInventorySub.getBeizhu());
                subvalues.put("state", mInventorySub.getState());
                subvalues.put("cheadguid", rowid);
                db.insert(INVENTORYTASKSUB, null, subvalues);
            }
        }
        return 1;
    }

    /**
     * 客服端 存储任务 数据
     */
    public int insertTaskData(List<InventoryTask> mInventoryTask) {
        for (int i = 0; i < mInventoryTask.size(); i++) {
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
            ContentValues values = new ContentValues();
            values.put("code", mInventoryTask.get(i).getCode());
            values.put("name", mInventoryTask.get(i).getName());
            values.put("range", mInventoryTask.get(i).getRange());
            values.put("spQty", mInventoryTask.get(i).getSpQty());
            values.put("diffQty", mInventoryTask.get(i).getDiffQty());
            values.put("state", mInventoryTask.get(i).getState());
            values.put("crDate", mInventoryTask.get(i).getCrDate());
            long rowid = db.insert(INVENTORYTASK, null, values);
            for (int j = 0; j < mInventoryTask.get(i).getInventoryTaskSub().size(); j++) {
                InventoryTaskSub inventoryTaskSub = mInventoryTask.get(i).getInventoryTaskSub().get(i);
                ContentValues subvalues = new ContentValues();
                subvalues.put("location", inventoryTaskSub.getLocation());
                subvalues.put("barode", inventoryTaskSub.getBarode());
                subvalues.put("goods", inventoryTaskSub.getGoods());
                subvalues.put("code", inventoryTaskSub.getCode());
                subvalues.put("goodsName", inventoryTaskSub.getGoodsName());
                subvalues.put("color", inventoryTaskSub.getColor());
                subvalues.put("sizeName", inventoryTaskSub.getSizeName());
                subvalues.put("type", inventoryTaskSub.getType());
                subvalues.put("maitype", inventoryTaskSub.getMaitype());
                subvalues.put("bookNumber", inventoryTaskSub.getBookNumber());
                subvalues.put("sjnumber", inventoryTaskSub.getSjnumber());
                subvalues.put("diffNumber", inventoryTaskSub.getDiffNumber());
                subvalues.put("beizhu", inventoryTaskSub.getBeizhu());
                subvalues.put("state", inventoryTaskSub.getState());
                subvalues.put("cheadguid", rowid);
                db.insert(INVENTORYTASKSUB, null, subvalues);
            }
        }
        return 1;
    }


    /**
     * 盘点 提交 数据 数据存储
     */
    public long insertTsakSubData(List<InventorySub> mInventorySub) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        for (int i = 0; i < mInventorySub.size(); i++) {
            ContentValues subvalues = new ContentValues();
            subvalues.put("location", mInventorySub.get(i).getLocation());
            subvalues.put("barode", mInventorySub.get(i).getBarode());
            subvalues.put("goods", mInventorySub.get(i).getGoods());
            subvalues.put("code", mInventorySub.get(i).getCode());
            subvalues.put("goodsName", mInventorySub.get(i).getGoodsName());
            subvalues.put("color", mInventorySub.get(i).getColor());
            subvalues.put("sizeName", mInventorySub.get(i).getSizeName());
            subvalues.put("type", mInventorySub.get(i).getType());
            subvalues.put("maitype", mInventorySub.get(i).getMaitype());
            subvalues.put("sjnumber", mInventorySub.get(i).getSjnumber());
            subvalues.put("discNumber", mInventorySub.get(i).getDiscNumber());
            subvalues.put("moreThan", mInventorySub.get(i).getMoreThan());
            subvalues.put("lessThan", mInventorySub.get(i).getLessThan());
            subvalues.put("cheadguid", mInventorySub.get(i).getCheadguid());
            subvalues.put("state", mInventorySub.get(i).getState());
            db.insert(INVENTORYTASKSUB, null, subvalues);

        }
        return 1;
    }


    /**
     * 盘点  任务 完成 数据存储
     */
    public long insertSubData(InventorySub mInventorySub) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        ContentValues subvalues = new ContentValues();
        subvalues.put("location", mInventorySub.getLocation());
        subvalues.put("barode", mInventorySub.getBarode());
        subvalues.put("goods", mInventorySub.getGoods());
        subvalues.put("code", mInventorySub.getCode());
        subvalues.put("goodsName", mInventorySub.getGoodsName());
        subvalues.put("color", mInventorySub.getColor());
        subvalues.put("sizeName", mInventorySub.getSizeName());
        subvalues.put("type", mInventorySub.getType());
        subvalues.put("maitype", mInventorySub.getMaitype());
        subvalues.put("sjnumber", mInventorySub.getSjnumber());
        subvalues.put("discNumber", mInventorySub.getDiscNumber());
        subvalues.put("cheadguid", mInventorySub.getCheadguid());
        db.insert(INVENTORYSUB, null, subvalues);
        return 1;
    }
}
